<?php

/**
 * Controller_Default
 */


class Controller_default extends MyApp {

	function __construct ()
	{
		parent::__construct();
		$this->model->user   	= YOU::loadModel('user');
		$this->model->notice   	= YOU::loadModel('notice');
		$this->model->inform   	= YOU::loadModel('inform');
		$this->model->semester  = YOU::loadModel('semester');

	}

	/** 首页 - 综合数据**/
	function actionIndex ()
	{
		
		$user_id = $this->user['user_id'];
		$type_id = $this->user['type_id'];
		if ($this->user['user_type']=='repair') {
			Helper_Client::location("无权进入首页", "index.php?a=main.maintainlist&maintainlist_type=1");
		}

		if($user_id){
			$read_lists = $this->db->fetchAll("SELECT * FROM `tbl_inform_read` WHERE read_to= {$user_id} ORDER BY read_id ");
			if($read_lists) {
				$inform = array();
				foreach ($read_lists as $key => $value) {
					$inform[$key] = $value['inform_id'];
				}
				$informs = implode(',', $inform);//可以看到的信息的ID
				$canread = 'inform_id in (' . $informs . ')';
			}else {
				$canread = 'inform_id in (0)';
			}
			$read_lists = $this->db->fetchAll("SELECT * FROM `tbl_notice_read` WHERE read_to= {$user_id} ORDER BY read_id ");
			if($read_lists){
				$inform = array();
				foreach ($read_lists as $key => $value){
					$inform[$key] = $value['notice_id'];
				}
				$informs = implode(',',$inform);//可以看到的信息的ID
				$cannotice = 'notice_id in ('.$informs.')';
			}else{
				$cannotice = 'notice_id in (0)';
			}
			//echo $canread;
		}else{
			echo '错误：未登录！';exit;
		}

		$myname = $this->model->user->getNameById($user_id);

		$user_id = $this->user['user_id'];
		//echo $myname;
		//采购，维修人员与user对应目前不明！important
		// 报修 条数
		$main_type = '(1,2,3)';	//2是批准
		$main_lists = $this->db->fetchAll("SELECT * FROM `tbl_main` WHERE main_type in {$main_type} AND user_id = '{$this->user['user_id']}' ORDER BY main_id DESC LIMIT 0,5 ");
		$main_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_main` WHERE main_type in {$main_type} AND user_id = '{$this->user['user_id']}'");
		foreach($main_lists as $key =>$value){
			$main_lists[$key]['create_time'] = date('m-d H:i',strtotime($value['create_time']));
		}

		// 申购 条数
		$shop_type = '(1,2,3)';
		$shop_lists = $this->db->fetchAll("SELECT * FROM `tbl_shop` WHERE shop_type in {$shop_type} AND `user_id` = '{$this->user['user_id']}' ORDER BY shop_id DESC LIMIT 0,5");
		$shop_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_shop` WHERE shop_type in {$shop_type} AND `user_id` = '{$this->user['user_id']}'");
		foreach($shop_lists as $key =>$value){
			$shop_lists[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
		}

		// 领用 条数
		$goods_lists = $this->db->fetchAll("SELECT * FROM `tbl_goods` WHERE `goods_name` = '{$myname}' ORDER BY goods_id DESC LIMIT 0,5");
		$goods_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_goods` WHERE `goods_name` = '{$myname}' ");
		foreach($goods_lists as $key =>$value){
			$goods_lists[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
		}

		// 通知全部 条数
		// 前三行 上级通知
		$num = 3;
		$time = date('Y-m-d H:i',time());
		$inform_all_three = $this->model->notice->getIndexTwo($user_id,$num,$time);
		$inform_all_three_count = $this->model->notice->getIndexTowCount($user_id,$time);
		
		// 后二行公告
		// 公告 条数
		//$inform_type=2;
		$notice_type = 1;
		$num = 2;
		$inform_all_two = $this->model->notice->getIndexOne($user_id,$notice_type,$num);
		$inform_all_two_count = $this->model->notice->getIndexOneCount($user_id,$notice_type);
		

		// 通知 条数5（调取的是校长室的通知，不需要审核,通用事项发布通知审核过的）

		$num = 5;
		$time = date('Y-m-d H:i',time());
		$inform_lists_one = $this->model->notice->getIndexTwo($user_id,$num,$time);
		$inform_content_one = $this->model->notice->getIndexTowCount($user_id,$time);


		// 公告 条数5
		//$inform_type=2;

		$notice_type = 1;
		$num = 5;
		$inform_lists_two = $this->model->notice->getIndexOne($user_id,$notice_type,$num);
		$inform_content_two = $this->model->notice->getIndexOneCount($user_id,$notice_type);

		// 通知审核
		$notice_type = 3;
		$inform_lists_three = $this->model->notice->getIndexThree($type_id,$notice_type,$num);
		$inform_content_three = $this->model->notice->getIndexThreeCount($type_id,$notice_type);
		if (empty($inform_content_three)) {
			
			$inform_content_three['count'] = 0;
		}



		// 事务交代
		$notice_type = 5;
		$inform_type = 1;
		
		$one  = $this->model->notice->getIndexFour($user_id,$notice_type,$num);
		$one_cont = $this->model->notice->getIndexFourCount($user_id,$notice_type);
		
		// 上级通知
		$two = $this->model->inform->getIndexlist($user_id,$inform_type,$num);
		$two_cont = $this->model->inform->getIndexcount($user_id,$inform_type,$num);
		
		$inform_content_four = $one_cont['count']+$two_cont;
		$inform_lists_four= array_merge($one,$two);
		
		// 排序
		/*$sort = array(
		'direction' => 'SORT_DESC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'field'     => 'create_time',        //排序字段
		);*/
		$sorts = array(
		'no' 		=> 'SORT_DESC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'read'      => 'is_read',            //排序字段
		);
		$arrSort = array();
		foreach($inform_lists_four AS $uniqid => $row){
			foreach($row AS $key=>$value){
				$arrSort[$key][$uniqid] = $value;
			}
		}
		
		if($sorts['no']){
			@array_multisort($arrSort[$sort['read']], constant($sort['no']), $inform_lists_four);
		}
		/*if($sort['direction']){
			@array_multisort($arrSort[$sort['field']], constant($sort['direction']), $inform_lists_four);
		}*/
		
		// 显示5个
		foreach ($inform_lists_four as $key => $value){
			if ($key >=5) {
			
				unset($inform_lists_four[$key]);
			}
		}
		
		// 提醒

		$num  = 5;

		$one  = $this->model->notice->getIndexSix($user_id, $notice_type, $num);
		$one_cont  = $this->model->notice->getIndexSixCount($user_id, $notice_type, $num);
		// 上级通知
		$two = $this->model->inform->getIndexSixlist($user_id,$inform_type,$num);
		$two_cont = $this->model->inform->getIndexSixCount($user_id,$inform_type,$num);
		
		$inform_content   = $one_cont['count']+$two_cont['count'];
		$inform_lists     = array_merge($one,$two);
		
		// 排序
		$sorts = array(
		'no' 		=> 'SORT_ASC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'read'      => 'is_read',            //排序字段
		);
		$sort = array(
		'direction' => 'SORT_DESC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'field'     => 'create_time',        //排序字段
		);
		
		$arrSort = array();
		foreach($inform_lists AS $uniqid => $row){
			foreach($row AS $key=>$value){
				$arrSort[$key][$uniqid] = $value;
			}
		}
		if($sorts['no']){
			@array_multisort($arrSort[$sort['read']], constant($sort['no']), $inform_lists);
		}
		if($sort['direction']){
			@array_multisort($arrSort[$sort['field']], constant($sort['direction']), $inform_lists);
		}
		
		// 显示5个
		foreach ($inform_lists as $key => $value){
			if ($key >=5) {
			
				unset($inform_lists[$key]);
			}
		}		
		
		// 内部邮件 条数
		$mail_lists = $this->db->fetchAll("SELECT * FROM `tbl_mail` WHERE mail_to='{$this->user['user_id']}' ORDER BY mail_id DESC LIMIT 0,5");
		$mail_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_mail` WHERE mail_to='{$this->user['user_id']}' AND is_read='0'");
		
		// 通知部门id
		foreach($mail_lists as $key => $value){
			$mail_lists[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
			// 发件人
			$row  = $this->model->user->getinfo($value['mail_from']);
			$staff_data = $this->db->fetchRow("SELECT * FROM `tbl_staff` WHERE staff_id={$row['type_id']}");
			$mail_lists[$key]['staff_name'] = $staff_data['staff_name'];
		}

		// 工作全部
		// 第一行 周工作

		// 当然学期、周次
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
		$time = date('Y-m-d',time());
		$plan_week  = $this->db->fetchRow("SELECT * FROM `tbl_plan` WHERE start_time = '{$time}'");

		//第二行 学期周历表


		//第三 ~ 五行 日常工作
		$arrange_type = 3;

		$work_three = $this->db->fetchAll("SELECT * FROM `tbl_arrange` WHERE arrange_type ='{$arrange_type}' ORDER BY arrange_id DESC LIMIT 0,3");
		$work_three_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_arrange` WHERE arrange_type ='{$arrange_type}' ");
		foreach($work_three as $key => $value){
			$work_three[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));

		}

		// 周工作 ()
		$plan_lists = $this->db->fetchAll("SELECT distinct `plan_week`,create_time FROM `tbl_plan` ORDER BY plan_id DESC LIMIT 0,5");
		$plan_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_plan` ");
		foreach($plan_lists as $key => $value){
			$plan_lists[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));

		}

		// 日常工作
		$arrange_type = 3;

		$arrange_lists = $this->db->fetchAll("SELECT * FROM `tbl_arrange` WHERE arrange_type ='{$arrange_type}' ORDER BY arrange_id DESC LIMIT 0,5");
		$arrange_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_arrange` WHERE arrange_type ='{$arrange_type}' ");
		foreach($arrange_lists as $key => $value){
			$arrange_lists[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));

		}

		// 周历表

		$work_lists = $this->db->fetchAll("SELECT *  FROM `tbl_semester` ORDER BY semester_id DESC LIMIT 0,5");

		$work_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_week_work`");
		foreach($work_lists as $key => $value){
			$work_lists[$key]['create_time'] = date('m-d H:i',strtotime($value['create_time']));
			// 学年名称
			$work_lists[$key]['semester_name'] = $this->semester_name[$value['semester_name']];

		}
		//学生行为
		$students_lists = $this->db->fetchAll("SELECT * FROM `tbl_award_stu` ORDER BY stu_id DESC LIMIT 0,5");
		foreach($students_lists as $key => $value){
			$students_lists[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
			$students_data = $this->db->fetchRow("SELECT `student_name` from tbl_student WHERE student_id ={$value['student_id']}");
			$students_lists[$key]['student_name'] = $students_data['student_name'];
			$award_data = $this->db->fetchRow("SELECT `award_title` from tbl_award WHERE award_id ={$value['award_id']}");
			$students_lists[$key]['award'] = $award_data['award_title'];
		}
		//班级行为
		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_award_clas` ORDER BY clas_id DESC LIMIT 0,5");
		foreach($class_lists as $key => $value){
			$class_lists[$key]['create_time'        ] =date('m-d H:i',strtotime($value['create_time']));
			$class_data = $this->db->fetchRow("SELECT `class_name` from tbl_class WHERE class_id ={$value['class_id']}");
			$class_lists[$key]['class_name'] = $class_data['class_name'];
			$award_data = $this->db->fetchRow("SELECT `award_title` from tbl_award WHERE award_id ={$value['award_id']}");
			$class_lists[$key]['award'] = $award_data['award_title'];
		}

		$this->tpl->assign("students_lists", $students_lists);
		$this->tpl->assign("class_lists", $class_lists);

		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->assign("main_content", $main_content);

		$this->tpl->assign("shop_lists", $shop_lists);
		$this->tpl->assign("shop_content", $shop_content);

		$this->tpl->assign("goods_lists", $goods_lists);
		$this->tpl->assign("goods_content", $goods_content);

		$this->tpl->assign("inform_lists", $inform_lists);
		$this->tpl->assign("inform_content", $inform_content);

		$this->tpl->assign("inform_all_three", $inform_all_three);
		$this->tpl->assign("inform_all_three_count", $inform_all_three_count);

		$this->tpl->assign("inform_all_two", $inform_all_two);
		$this->tpl->assign("inform_all_two_count", $inform_all_two_count);

		$this->tpl->assign("inform_lists_three", $inform_lists_three);
		$this->tpl->assign("inform_content_three", $inform_content_three);

		$this->tpl->assign("inform_lists_two", $inform_lists_two);
		$this->tpl->assign("inform_content_two", $inform_content_two);

		$this->tpl->assign("inform_lists_one", $inform_lists_one);
		$this->tpl->assign("inform_content_one", $inform_content_one);

		$this->tpl->assign("inform_lists_four", $inform_lists_four);
		$this->tpl->assign("inform_content_four", $inform_content_four);

		$this->tpl->assign("mail_lists", $mail_lists);
		$this->tpl->assign("mail_content", $mail_content);

		$this->tpl->assign("work_one", $work_one);
		$this->tpl->assign("work_one_content", $work_one_content);

		$this->tpl->assign("work_two", $work_two);
		$this->tpl->assign("work_two_content", $work_two_content);

		$this->tpl->assign("work_three", $work_three);
		$this->tpl->assign("work_three_content", $work_three_content);

		$this->tpl->assign("plan_lists", $plan_lists);
		$this->tpl->assign("plan_content", $plan_content);

		$this->tpl->assign("work_lists", $work_lists);
		$this->tpl->assign("work_content", $work_content);

		$this->tpl->assign("arrange_lists", $arrange_lists);
		$this->tpl->assign("arrange_content", $arrange_content);

		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("plan_week", $plan_week);
		$this->tpl->assign("time", $time);

		$this->tpl->display();
	}
	public  function  actionsearch()
	{
		if ($this->user['user_type']=='repair') {
			Helper_Client::location("无权操作", "index.php?a=main.maintainlist&maintainlist_type=1");
		}	
		//    $arr = array(
		//        array(
		//            'id'   => 1,
		//            'name' => '张三',
		//            'age'  => 25,
		//        ),
		//        array(
		//            'id'   => 2,
		//            'name' => '李四',
		//            'age'  => 23,
		//        ),
		//        array(
		//            'id'   => 3,
		//            'name' => '王五',
		//            'age'  => 40,
		//        ),
		//        array(
		//            'id'   => 4,
		//            'name' => '赵六',
		//            'age'  => 31,
		//        ),
		//        array(
		//            'id'   => 5,
		//            'name' => '黄七',
		//            'age'  => 20,
		//        ),
		//    );
		//
		//
		//    $sort = array(
		//        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		//        'field'     => 'age',       //排序字段
		//    );
		//    $arrSort = array();
		//    foreach($arr AS $uniqid => $row){
		//        foreach($row AS $key=>$value){
		//            $arrSort[$key][$uniqid] = $value;
		//        }
		//    } var_dump($arr);exit;
		//    if($sort['direction']){
		//        array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arr);
		//    }



		$things =  $this->get->search;

		if (!empty($things)){

			$uid = $this->user['user_id'];
			$utype = $this->user['user_type'];
			$this->tpl->assign('utype',$utype);

			//通知
			$inform_list = $this->db->fetchAll("select a.* from `tbl_inform` as a ,`tbl_inform_read` as b WHERE a.inform_title LIKE '%{$things}%' and ((b.read_to ='{$uid}' AND a.inform_id = b.inform_id) OR a.user_id='{$uid}')");
			//notice公告事物交代
			$notice_list = $this->db->fetchAll("select DISTINCT a.* from `tbl_notice` as a ,`tbl_notice_read` as b WHERE a.notice_title LIKE '%{$things}%' and ((b.read_to ='{$uid}' AND a.notice_id = b.notice_id) OR a.user_id='{$uid}')");


			//    $sort = array(
			//        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
			//        'field'     => 'create_time',       //排序字段
			//    );
			//    $arrSort = array();
			//    foreach($arr AS $uniqid => $row){
			//        foreach($row AS $key=>$value){
			//            $arrSort[$key][$uniqid] = $value;
			//        }
			//    }
			//    if($sort['direction']){
			//        @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arr);
			//    }
			//    //array_multisort($arr['create_time'],constant(SORT_DESC),$arr);
			//
			//    $sort = array(
			//        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
			//        'field'     => 'create_time',       //排序字段
			//    );
			//    $arrSort = array();
			//    foreach($arr AS $uniqid => $row){
			//        foreach($row AS $key=>$value){
			//            $arrSort[$key][$uniqid] = $value;
			//        }
			//    }
			//    if($sort['direction']){
			//        array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arr);
			//    }
			//array_multisort($arr['create_time'],constant(SORT_DESC),$arr);

			//文件
			$file_list = $this->db->fetchAll("select * from tbl_file where file_name LIKE '%{$things}%' and (is_open = 1 or user_id = {$uid})");
			//var_dump($file_list);
			//教学资源
			$resource_list = $this->db->fetchAll("select * from `tbl_resource`  WHERE resource_title LIKE '%{$things}%' ");
			//示范课教案
			$manage_list = $this->db->fetchAll("select * from `tbl_manage`  WHERE manage_title LIKE '%{$things}%' ");
			//学科活动
			$subactivitys_list = $this->db->fetchAll("select * from `tbl_activitys`   WHERE activitys_type='1'AND activitys_title LIKE '%{$things}%' ");
			$achieve_list = $this->db->fetchAll("select * from `tbl_achieve`   WHERE  achieve_name LIKE '%{$things}%' ");


			$arr = array_merge($inform_list,$notice_list,$file_list,$resource_list,$manage_list,$subactivitys_list,$achieve_list);

			$sort = array(
			'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
			'field'     => 'create_time',       //排序字段
			);
			$arrSort = array();
			foreach($arr AS $uniqid => $row){
				foreach($row AS $key=>$value){
					$arrSort[$key][$uniqid] = $value;
				}
			}
			if($sort['direction']){
				@array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arr);
			}
			//array_multisort($arr['create_time'],constant(SORT_DESC),$arr);

			$lists = array_slice($arr,0,30);
		}
		$this->tpl->assign('things',$things);
		$this->tpl->assign('lists',$lists);
		//    $this->tpl->assign('inform_list',$inform_list);
		//    $this->tpl->assign('notice_list',$notice_list);
		//    $this->tpl->assign('file_list',$file_list);
		//    $this->tpl->assign('resource_list',$resource_list);
		//    $this->tpl->assign('manage_list',$manage_list);
		//    $this->tpl->assign('subactivitys_list',$subactivitys_list);
		$this->tpl->display('search.index.tpl.php');

	}



}



?>